﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PlanProd.Contracts.Search;
using Web.Framework.Svc;
using System.Collections;
using Web.Framework.Search;

namespace WebAppPlanProduccion.Code.Search
{
    [Serializable]
    public class ProductoSearchObj : IGridViewSearchObjectDataSource
    {
        public WebServiceHelper WebServiceHelper { get; set; }

        public SearchProductCriteria SearchParam { get; set; }        

        public SearchProductResult SearchResult { get; set; }

        public ServiceOptions ServiceOptions { get; set; }

        public IList GetObjects()
        {
            return GetObjects(int.MaxValue, 0, string.Empty);
        }

        public IList GetObjects(int maximumRows, int startRowIndex)
        {
            return GetObjects(maximumRows, startRowIndex, string.Empty);
        }

        public IList GetObjects(string sortExpression)
        {
            return GetObjects(int.MaxValue, 0, sortExpression);
        }

        public int TotalNumberOfGetObjects()
        {
            return SearchResult.Count;
        }

        public IList GetObjects(int maximumRows, int startRowIndex, string sortExpression)
        {
            if (WebServiceHelper != null && SearchParam != null)
            {                
                SearchParam.PageSize = maximumRows;
                SearchParam.PageSkip = startRowIndex;

                //SearchParam.SortingField = sortExpression;
                //if (String.IsNullOrEmpty(SearchParam.SortingField))
                //{
                //    SearchParam.SortingField = "Id";
                //    SearchParam.SortIsAsc = true;
                //}

                SearchResult = WebServiceHelper.SearchProducts(ServiceOptions, SearchParam);
                return SearchResult.Productos;
            }
            else
            {
                SearchResult = new SearchProductResult();
                return new List<int>(); //Devuelvo una lista vacía
            }
        }
    }
}